// created on 22.07.2007 at 14:34
using System;
using System.Collections;
using Designer;
using Gtk;
public class ActionSet
	{
		public class SetTab
		{
			ListStore actionsStore;
			Label label;
			Gtk.ScrolledWindow scrolledwindow;
			Gtk.TreeView setTree;
			public SetTab(Notebook nb, ActionSet aset)
			{
		        scrolledwindow = new Gtk.ScrolledWindow();
		        scrolledwindow.CanFocus = true;
		        scrolledwindow.Name = "scrolledwindow";
		        scrolledwindow.VscrollbarPolicy = ((Gtk.PolicyType)(1));
		        scrolledwindow.HscrollbarPolicy = ((Gtk.PolicyType)(1));
		        scrolledwindow.Visible = true;
		        //Container child scrolledwindow3.Gtk.Container+ContainerChild
		        setTree = new Gtk.TreeView();
		        setTree.CanFocus = true;
		        setTree.Name = "actionTree";
		        setTree.Visible = true;
		        setTree.ModifyBase(StateType.Normal, new Gdk.Color(237,233,227));
		        setTree.ShowAll();
		        scrolledwindow.Add(this.setTree);
		        nb.Add(this.scrolledwindow);
		        Gtk.Notebook.NotebookChild w51 = ((Gtk.Notebook.NotebookChild)(nb[this.scrolledwindow]));
		        w51.TabExpand = false;
		        // Notebook tab
				label = new Gtk.Label();
		        label.Name = "label";
		        label.LabelProp = aset.name;
		        label.Angle = -90;
		        nb.SetTabLabel(this.scrolledwindow, this.label);
		        
		        TreeViewColumn actionsColumn = new TreeViewColumn ();
				actionsColumn.Title = "Aktionsgruppe: "+aset.name;
				CellRendererText actionsNameCell = new CellRendererText ();
				actionsColumn.PackStart (actionsNameCell, true);
				actionsStore = new ListStore (typeof (Designer.Action));
				setTree.Model = actionsStore;
		 		setTree.AppendColumn (actionsColumn);
		 		actionsColumn.SetCellDataFunc(actionsNameCell, new Gtk.TreeCellDataFunc (RenderAction));
		 		setTree.DragDataGet+=OnDragDataGet;
		 		foreach(Designer.Action act in aset.actions)
		 		{
		 			actionsStore.AppendValues(act);
		 		}		
		 		setTree.EnableModelDragSource(Gdk.ModifierType.Button1Mask, new TargetEntry[] {new TargetEntry("STRING",0, 0)}, Gdk.DragAction.Copy);
			}
			private void RenderAction (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
			{
				Designer.Action act = (Designer.Action) model.GetValue (iter, 0);
				(cell as CellRendererText).Text = act.Title;
			}
			protected void OnDragDataGet(object o, Gtk.DragDataGetArgs args)
			{
				TreeSelection sel = setTree.Selection;
				TreeIter iter;
				sel.GetSelected(out iter);
				Designer.Action act = (Designer.Action)actionsStore.GetValue(iter, 0);
				args.SelectionData.Text = act.Type;
			}
		}
		public SetTab GetTab(Notebook nb)
		{
			return new SetTab(nb, this);
		}
		public string name;
		protected ArrayList actions = new ArrayList();
		public ActionSet(string name)
		{
			this.name = name;
//			for(int i = 1; i < 5; i++)
//	 			AddAction(name+" Test Action "+i.ToString());
		} 
		public void AddAction(string title)
		{
			Designer.Action action = new Designer.Action(title, "", this);
			actions.Add(action);
			//TODO: has to add edit window widget
			ActionEditWins.Add(action, null);
		}
		public Hashtable ActionEditWins = new Hashtable();
		public void OpenActionEditWin(Designer.Action act, MainWindow win)
		{
			if(act.Type == "Code" && this.name == "Standard")
			{
				Console.WriteLine("Opening Code Window for editing action "+act.Type);
				win.ShowEditTab((Code)((ArrayList)act.Args[0])[1]);
			}
			else
			{
//				ActionEditWin editor = (ActionEditWin) ActionEditWins[act];
//				editor.OpenWindow();
				Console.WriteLine("[NOT IMPLEMENTED] Open Window for editing action "+act.Type);
			}
		}
	}
	interface ActionEditWin
	{
		void OpenWindow();
	}